//
//  GameStart.swift
//  Shooting Gallery
//
//  Created by chen on 2019/12/23.
//  Copyright © 2019 Leonard Chan. All rights reserved.
//

import UIKit
import SpriteKit
import GameKit

class GameStart: SKScene ,GKGameCenterControllerDelegate{
    
    var model: SKSpriteNode!
    
    func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) {
        gameCenterViewController.dismiss(animated: true) {
            
        }
    }
    
    override func didMove(to view: SKView) {
        self.authenticateLocalGamePlayer()
        self.initLocalPlayer()
        self.layouButtons()
    }
    
    func authenticateLocalGamePlayer(){
        let localPlayer = GKLocalPlayer.local
        localPlayer.authenticateHandler = {(viewController, error) -> Void in
            if viewController != nil {
                if self.view != nil{
                    let vc: UIViewController = self.view!.window!.rootViewController!
                    vc.present(viewController!, animated: true, completion: nil)
                }
            }
            else {
                print((GKLocalPlayer.local.isAuthenticated))
            }
        }
    }

    func showLeader() {
        let vc = self.view?.window?.rootViewController
        let gc = GKGameCenterViewController()
        gc.gameCenterDelegate = self as GKGameCenterControllerDelegate
        vc?.present(gc, animated: true, completion: nil)
    }
    
    func initLocalPlayer(){
        CJDPlayerHandler.sharedInstance.play(fileName: Sound.musicLoop.fileName, vol: 0.3,numberOfLoops: -1)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for toucIn in touches {
            let pt = toucIn.location(in: self.scene!)
            let nodes = self.nodes(at: pt)
            for nodeItem in nodes {
                if nodeItem.name == "mute" {
                    CJDPlayerHandler.sharedInstance.removeVoice(key: "warning")
                    CJDPlayerHandler.sharedInstance.removeVoice(key: "score")
                    CJDPlayerHandler.sharedInstance.removeVoice(key: "reload")
                    CJDPlayerHandler.sharedInstance.removeVoice(key: "hit")
                    CJDPlayerHandler.sharedInstance.resumeAllVoice()
                    let nodeImg = nodeItem as! SKSpriteNode
                    nodeItem.run(SKAction.setTexture(SKTexture.init(imageNamed: "yesvoice")))
                    nodeImg.name = "yesvoice"
                    PlayHandler.sharedInstance.isPlay = true
                }else if nodeItem.name == "yesvoice" {
                    CJDPlayerHandler.sharedInstance.stopAllVoice()
                    let nodeImg = nodeItem as! SKSpriteNode
                    nodeImg.run(SKAction.setTexture(SKTexture.init(imageNamed:"mute")))
                    nodeImg.name = "mute"
                    PlayHandler.sharedInstance.isPlay = false
                }else if nodeItem.name == "start" {
                    if let gameStart = SKScene(fileNamed: "StageScene") {
                        gameStart.scaleMode = .aspectFill
                        self.view?.presentScene(gameStart,transition: SKTransition.doorsOpenVertical(withDuration: 1))
                    }
                }
                    else if nodeItem.name == "list" {
                        showLeader()
                }
            }
        }
    }
    
    func layouButtons(){
        
        let mainImage = SKSpriteNode(texture: SKTexture(imageNamed: "main"))
        mainImage.position = CGPoint(x: 406, y: 256)
        mainImage.zPosition = 12
        self.addChild(mainImage)
        
        
        let restartNode1 = SKSpriteNode(texture: SKTexture(imageNamed: "start"))
        restartNode1.position = CGPoint(x: 406, y: 170)
        restartNode1.zPosition = 12
        restartNode1.setScale(0.9)
        restartNode1.name = "start"
        self.addChild(restartNode1);
        
        let leftButtonNode = SKSpriteNode(texture: SKTexture(imageNamed: "yesvoice"))
        leftButtonNode.position = CGPoint(x: 170, y: 300)
        leftButtonNode.zPosition = 12
        leftButtonNode.setScale(1.1)
        leftButtonNode.name = "yesvoice"
        self.addChild(leftButtonNode);
        

        let gameButtonNode = SKSpriteNode(texture: SKTexture(imageNamed: "list"))
        gameButtonNode.position = CGPoint(x: 690, y: 320)
        gameButtonNode.zPosition = 12
        gameButtonNode.setScale(1.1)
        gameButtonNode.name = "list"
        self.addChild(gameButtonNode);
    }
}
